package com.probiz.estore.common.service.impl;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.probiz.estore.cart.util.ShoppingCartUtil;
import com.probiz.estore.common.model.cart.Shoppingcart;
import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.model.system.AppUser;
import com.probiz.estore.common.model.system.LoginEntry;
import com.probiz.estore.common.service.CustomerService;
import com.probiz.estore.common.service.LoginNtService;
import com.probiz.estore.core.util.CacheUtil;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.webapp.util.RequestContext;
import com.probiz.estore.webapp.util.RequestUtil;
import com.probiz.estore.webapp.util.SessionUtil;

public class LoginNtServiceImpl implements LoginNtService{
	private CustomerService customerService;

	private CustomerManager customerManager;

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public void setCustomerManager(CustomerManager customerManager) {
		this.customerManager = customerManager;
	}
	/**
	 * 增加登录cookies
	 * @param request
	 * @param response
	 */
	private void addLoginedFlag(HttpServletRequest request, HttpServletResponse response) {
		RequestUtil.setUserIdCookie(response, RequestContext.getCurrentUserId(), (request).getContextPath());
		RequestUtil.setUserNameCookie(response, RequestContext.getCurrentUser().getUsername(), (request).getContextPath());
	}
	/**
	 * 登录后重新计算购物车
	 * @param appUser
	 */
	private void refreshShoppingCart(AppUser appUser){
		Shoppingcart shoppingcart = ShoppingCartUtil.getInstance().getCurrentUserShoppingcart();
		shoppingcart.setOwner(appUser);
		shoppingcart.refresh();
	}
	/**
	 * 设置上次登录时间
	 * @param request
	 * @param appUser
	 */
	private void setLastLoginTime(HttpServletRequest request,AppUser appUser){
		if (RequestContext.isFront()) {
			Customer customer = (Customer) appUser;
			SessionUtil.setAttribute(request.getSession(), "lastLoginTime", customer.getLastLoginTime());
			customer.setLastLoginTime(new Date());
			customerService.saveCustomer(customer);
			customerManager.evict(customer);
		}
	}
	/**
	 * 登录次数清零
	 * @param request
	 * @param response
	 * @param appUser
	 */
	private void resetLoginCount(HttpServletRequest request,AppUser appUser){
		LoginEntry entry = (LoginEntry) CacheUtil.getInstance().getLoginErrorCache(request.getRemoteAddr());
		if (entry != null && appUser != null) {
			entry.setVisitedTimes(0);
		}
	}
	
	/**
	 * 登录后台
	 * @param request
	 * @param appUser
	 */
	private void afterloginStoreAdmin(HttpServletRequest request,HttpServletResponse response, AppUser appUser){
		addLoginedFlag(request,response);
		resetLoginCount(request,appUser);
	}
	/**
	 * 登录前台
	 * @param request
	 * @param response
	 * @param appUser
	 */
	private void afterloginFront(HttpServletRequest request,HttpServletResponse response, AppUser appUser){
		addLoginedFlag(request,response);
		refreshShoppingCart(appUser);
		setLastLoginTime(request,appUser);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.common.service.LoginNtService#afterLogin(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, com.probiz.estore.common.model.system.AppUser)
	 */
	public void afterLogin(HttpServletRequest request,HttpServletResponse response, AppUser appUser){
		if (RequestContext.isFront()) {
			afterloginFront(request, response, appUser);
		} else {
			afterloginStoreAdmin(request, response, appUser);
		}
	}
	
}
